package com.sun.stringescapeutils;

import java.util.Timer;
import java.util.TimerTask;

public class TimerDemo {

    private Timer timer;
    private TimerTask timerTask;

    public void doTask(){
        timer = new Timer("myTimer");
        timerTask = new TimerTask() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
                System.out.println("before timer cancel");
//                timer.cancel();
                System.out.println("after timer cancel");
            }
        };
        //延迟delay毫秒后，执行第一次task，然后每隔period毫秒执行一次task。
        timer.schedule(timerTask,0,2*1000);
    }
    public static void main(String[] args) {
        TimerDemo demo = new TimerDemo();
        demo.doTask();
    }
}
